package com.liang.config;

import net.bytebuddy.asm.MemberSubstitution;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import sun.net.idn.Punycode;

/**
 * Created with IntelliJ IDEA.
 * User: XIAOLIANG
 * Date: 2021/6/18
 * Time: 23:21
 * Description: 一个延迟队列的配置
 */
@Configuration
public class RabbitMqConfig {


    //一个普通交换机

    public static final String X_EXCHANGE = "X";

    //一个死信交换机
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";

    //两个 普通队列
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";

    //声明 QC 队列 没有ttl
    public static final String QUEUE_C = "QC";

    //一个死信队列
    public static final String DEAD_LETTER_QUEUE = "QD";


    //声明 X_EXCHANGE

    @Bean(X_EXCHANGE)
   public DirectExchange xExchange(){
        return new DirectExchange(X_EXCHANGE);
    }

    @Bean(Y_DEAD_LETTER_EXCHANGE)
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    //声明一个普通的队列 10秒过期
    @Bean("QA")
    public Queue queueA(){
        return QueueBuilder.durable(QUEUE_A).ttl(10000).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE).deadLetterRoutingKey("YD").build();
    }

    //声明一个普通的队列  40秒过期
    @Bean("QB")
    public Queue queueB(){
        return QueueBuilder.durable(QUEUE_B).ttl(40000).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE).deadLetterRoutingKey("YD").build();
    }

    //声明QC队列
    @Bean("QC")
    public Queue queueC(){

        return QueueBuilder.durable(QUEUE_C).deadLetterExchange(Y_DEAD_LETTER_EXCHANGE).deadLetterRoutingKey("YD").build();

    }

    //声明一个死信队列
    @Bean(DEAD_LETTER_QUEUE)
    public Queue queueD(){
        return QueueBuilder.durable(DEAD_LETTER_QUEUE).build();
    }


    //绑定关系
    @Bean
    public Binding queueA_BindingX(@Qualifier("QA") Queue QA,@Qualifier(X_EXCHANGE) DirectExchange directExchange){

        return BindingBuilder.bind(QA).to(directExchange).with("XA");

    }

    @Bean
    public Binding queueB_BindingX(@Qualifier("QB") Queue QB,@Qualifier(X_EXCHANGE) DirectExchange directExchange){

        return BindingBuilder.bind(QB).to(directExchange).with("XB");

    }

    @Bean
    public Binding queueD_BindingY(@Qualifier(DEAD_LETTER_QUEUE) Queue QD,@Qualifier(Y_DEAD_LETTER_EXCHANGE) DirectExchange directExchange){

        return BindingBuilder.bind(QD).to(directExchange).with("YD");

    }

    @Bean
    public Binding queueC_BindingX(@Qualifier("QC") Queue QC,@Qualifier(X_EXCHANGE) DirectExchange directExchange){

        return BindingBuilder.bind(QC).to(directExchange).with("XC");

    }


}